package com.coco.controller;

import com.coco.config.JwtProperties;
import com.coco.entity.UserInfo;
import com.coco.model.UserInfoCondition;
import com.coco.service.UserService;
import com.coco.utils.CoPageResp;
import com.coco.utils.CookieUtils;
import com.coco.utils.JwtUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author GHOSTLaycoo
 * @date 2020/2/28 0028 - 下午 16:14
 */
@RestController
@EnableConfigurationProperties(JwtProperties.class)
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public ResponseEntity<String> selectByUserInfo(@RequestParam String username,@RequestParam String password,HttpServletRequest request,
                                                 HttpServletResponse response){

        String token = userService.selectByUserInfo(username,password);

        String id = null;
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
            id = infoFromToken.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(token);
        if(StringUtils.isBlank(token)){
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }

        CookieUtils.setCookie(request,response,this.jwtProperties.getCookieName(),token,this.jwtProperties.getExpire
                ()*60);
        return ResponseEntity.ok(id);
    }

    @ApiOperation(value = "分页查询用户信息")
    @PostMapping("/getUserByCondition")
    public ResponseEntity<CoPageResp<UserInfo>> getUserByCondition(@RequestBody UserInfoCondition queryInfo){
        CoPageResp<UserInfo> userByCondition = userService.getUserByCondition(queryInfo);
        return ResponseEntity.ok(userByCondition);
    }

    @ApiOperation(value = "保存用户信息")
    @PostMapping("/save")
    public ResponseEntity<Integer> save(@RequestBody UserInfo user){
        int result = userService.save(user);
        return ResponseEntity.status(HttpStatus.CREATED).body(result);
    }

    @ApiOperation(value = "更新用户信息")
    @PostMapping("/update")
    public ResponseEntity<Integer> update(@RequestBody UserInfo user){
        int result = userService.update(user);
        return ResponseEntity.status(HttpStatus.CREATED).body(result);
    }

    @ApiOperation(value = "永久删除用户信息")
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Integer> delete(@PathVariable("id") String id){
        int result = userService.delete(id);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "更新用户状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public ResponseEntity<Integer> updateStatus(@PathVariable("id") String id,@PathVariable("status") String status,HttpServletRequest request){
        int result = userService.updateStatus(id,status,request);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "查询用户详细信息")
    @GetMapping("/getUserInfoById/{id}")
    public ResponseEntity<UserInfo> getUserInfoById(@PathVariable("id") String id){
        UserInfo userInfoById = userService.getUserInfoById(id);
        return ResponseEntity.ok(userInfoById);
    }

    @ApiOperation(value = "查询用户详细信息")
    @GetMapping("/getUserInfoById")
    public ResponseEntity<UserInfo> getUserInfoById(HttpServletRequest request){
        String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
        try {
            UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
            UserInfo userInfoById = userService.getUserInfoById(infoFromToken.getId());
            return ResponseEntity.ok(userInfoById);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
